package nc.bs.pu.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.pu.bp.Ap2EasReqBp;
import nc.bs.pu.event.PUBusinessEvent.PUUserObj;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pub.BusinessException;

public class ApFile2EasListener implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		PUUserObj userObject = ((PUUserObj)event.getUserObject());
		InvoiceVO[] bills = (InvoiceVO[])userObject.getNewObjects();
		if(ArrayUtils.isNotEmpty(bills)) {
			List<String> pklist = new ArrayList<String>();
			for(InvoiceVO bill : bills) {
				if(StringUtils.isNotEmpty(bill.getPrimaryKey())) {
					pklist.add(bill.getPrimaryKey());
				}
			}
			
			if(pklist.size() > 0) {
				String[] pks = new String[pklist.size()]; pklist.toArray(pks);
				Ap2EasReqBp bp = new Ap2EasReqBp();
				bp.insert(pks);
				Thread thread = new Thread(new Runnable() {
		        	@Override
		        	public void run() {

		        		try {
							Thread.sleep(1000 * pks.length);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		        		bp.send(pks);
		        	}
		        });
		        thread.start();
			}
		}
	}

}
